package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;

import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.util.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService{

    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
          strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
       strategyCatalogRepository.deleteById(id);
    }

    @Override
    public StrategyCatalog get(String id) {
        return strategyCatalogRepository.findById(id).get();
    }

    @Override
    public List<StrategyCatalog> list() {
        return strategyCatalogRepository.findAll();
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        //查询对象
        Query query = new Query();

        Long totalCount = mongoTemplate.count(query, StrategyCatalog.class);

        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");

        query.with(pageable);


        List<StrategyCatalog> strategyCatalogs = mongoTemplate.find(query, StrategyCatalog.class);
        return new PageImpl(strategyCatalogs,pageable,totalCount);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        Destination destination = destinationService.get(strategyCatalog.getDestId());
        strategyCatalog.setDestName(destination.getName());
        if(StringUtils.hasLength(strategyCatalog.getId())){
            this.update(strategyCatalog);
        }else {
            this.save(strategyCatalog);
        }
    }

    @Override
    public List<CatalogVO> groupList() {
        ArrayList<CatalogVO> list = new ArrayList<>();
        //聚合查询
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,Aggregation.group("destName").
        push("name").as("names").push("id").as("ids"));
        AggregationResults<Map> results = mongoTemplate.aggregate(agg,Map.class);
        List<Map> datas = results.getMappedResults();
        for (Map data:datas){
            CatalogVO vo = new CatalogVO();
            vo.setDestName(data.get("_id").toString());
            //攻略分类的名称集合
            List<Object> names = (List<Object>) data.get("names");
            //攻略分类的ids集合
            List<Object> ids = (List<Object>) data.get("ids");

            //攻略集合对象
            List<StrategyCatalog> catalogList = new ArrayList<>();

            for (int i=0;i<names.size();i++){
                StrategyCatalog sc = new StrategyCatalog();
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                sc.setId(id);
                sc.setName(name);
                catalogList.add(sc);
            }
            vo.setList(catalogList);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<StrategyCatalog> findByDestId(String id) {

        return strategyCatalogRepository.findByDestId(id);
    }


}
